var TestHome = (function () {

	function initTree() {
		$("#jstree").on("select_node.jstree", function (e, data) {
			for (var i = 0; i < data.selected.length; i++) {
				selectedNode = $("#" + data.instance.get_node(data.selected[i]).id);
			}
		}).on("loaded.jstree", function (event, data) {
			$(this).jstree("open_all");
		}).jstree({
			"checkbox": {
				"real_checkboxes": true,
				"real_checkboxes_names": function (n) {
					return [n[0].id, 1];
				},
				"two_state": false
			},
			"themes": {
				"theme": "default",
				"url": "/../../lib/jstree/themes/default/style.css",
				"dots": false
			},
			"plugins": ["themes", "ui", "checkbox", "html_data"]
		});
		$("#jstree").css("background-color", "white");
	}


	function getCategories() {
		TestMgtService.getTestCategories(getCategories_Result);
	}

	function getCategories_Result(data) {
		var categoriesWrapper = {};
		categoriesWrapper.categories = data;
		alert(JSON.stringify(categoriesWrapper));
		var source = $("#categories-list").html();
		var templ = Handlebars.compile(source);
		$("#categoryTreeContainer").empty();
		$("#categoryTreeContainer").append("<div id='jstree'></div>");
		$("#jstree").append(templ(categoriesWrapper));
		initTree();
	}


	$(document).ready(function () {
		widget.init();
		Handlebars.registerPartial("categories-recursion", $("#categories-recursion-partial").html());
		getCategories();

		$("#saveInterests").on("click", function () {
			for (var i = 0; i < $(".jstree-checked").length; i++) {
				alert($(".jstree-checked").eq(i).data("name"));
			}
		});
	});


	return {
		getCategories_Result: getCategories_Result
	};

})();